package yao.com.exp4.activity;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import yao.com.exp4.R;

public class MainActivity extends Activity implements OnClickListener {

    private List<View> views = new ArrayList<View>();
    private ViewPager viewPager;
    private LinearLayout lactivity, lactivity_view, lperson_info;
    private ImageView img_activity, img_activity_view, img_person_info, ivCurrent;
    private TextView tvChat, tvFriends, tvContacts, tvCurrent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();

        initData();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        lactivity = (LinearLayout) findViewById(R.id.lactivity);
        lactivity_view = (LinearLayout) findViewById(R.id.lactivity_view);
        lperson_info = (LinearLayout) findViewById(R.id.lperson_info);

        lactivity.setOnClickListener(this);
        lactivity_view.setOnClickListener(this);
        lperson_info.setOnClickListener(this);

        img_activity = (ImageView) findViewById(R.id.img_activity);
        img_activity_view = (ImageView) findViewById(R.id.img_activity_view);
        img_person_info = (ImageView) findViewById(R.id.img_person_info);

        tvChat = (TextView) findViewById(R.id.tvChat);
        tvFriends = (TextView) findViewById(R.id.tvFriends);
        tvContacts = (TextView) findViewById(R.id.tvContacts);

        img_activity.setSelected(true);
        tvChat.setSelected(true);
        ivCurrent = img_activity;
        tvCurrent = tvChat;

        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                changeTab(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    private void initData() {
        LayoutInflater mInflater = LayoutInflater.from(this);
        View tab01 = mInflater.inflate(R.layout.tab01, null);
        View tab02 = mInflater.inflate(R.layout.tab02, null);
        View tab03 = mInflater.inflate(R.layout.tab03, null);
        views.add(tab01);
        views.add(tab02);
        views.add(tab03);

        MyPagerAdapter adapter = new MyPagerAdapter(views);
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        changeTab(v.getId());
    }

    private void changeTab(int id) {
        ivCurrent.setSelected(false);
        tvCurrent.setSelected(false);
        switch (id) {
            case R.id.lactivity:
                viewPager.setCurrentItem(0);
            case 0:
                img_activity.setSelected(true);
                ivCurrent = img_activity;
                tvChat.setSelected(true);
                tvCurrent = tvChat;
                break;
            case R.id.lactivity_view:
                viewPager.setCurrentItem(1);
            case 1:
                img_activity_view.setSelected(true);
                ivCurrent = img_activity_view;
                tvFriends.setSelected(true);
                tvCurrent = tvFriends;
                break;
            case R.id.lperson_info:
                viewPager.setCurrentItem(2);
            case 2:
                img_person_info.setSelected(true);
                ivCurrent = img_person_info;
                tvContacts.setSelected(true);
                tvCurrent = tvContacts;
                break;
            default:
                break;
        }
    }

}